package com.loyal.loyalerp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.humenresource.employee.data.Gy_PersonData;
import com.loyal.loyalerp.humenresource.employee.mapper.Gy_PersonMapper;
import com.loyal.loyalerp.mapper.EmployeeMapper;
import com.loyal.loyalerp.mapper.GyUserMapper;
import com.loyal.loyalerp.model.Employee;
import com.loyal.loyalerp.model.GyUser;
import com.loyal.loyalerp.model.vo.UserNameAndCode;
import com.loyal.loyalerp.service.EmployeeService;

@Service
public class EmployeeServiceImpl implements EmployeeService {
	@Autowired
	EmployeeMapper employeeMapper;
	@Autowired
	GyUserMapper gyUserMapper;
	@Autowired
	Gy_PersonMapper gyPersonMapper;

	@Override
	public PageInfo<Employee> listEmployee(Integer PageNum, Integer recordNum, String sord) {

		Page<Employee> page = PageHelper.startPage(PageNum, recordNum);
		List<Employee> list = employeeMapper.listSortedEmployee("id", sord);
		PageInfo<Employee> pageInfo = new PageInfo<>(page);
		return pageInfo;
	}

	@Override
	public boolean editEmployee(String oper, Employee employee) {
		if (oper.equals("add")) {
			try {
				employeeMapper.insertSelective(employee);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("del")) {
			try {
				employeeMapper.deleteByPrimaryKey(employee.getId());
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("edit")) {
			try {
				employeeMapper.updateByPrimaryKeySelective(employee);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return false;
	}

	@Override
	public Employee findEmployee(Long id) {
		// TODO Auto-generated method stub
		return employeeMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo<Employee> findEmployee(Integer PageNum, Integer recordNum, String sord, String searchField,
			String searchString) {
		// TODO Auto-generated method stub
		Page<Employee> page = PageHelper.startPage(PageNum, recordNum);
		List<Employee> list = employeeMapper.findEmployee(searchField, searchString);
		PageInfo<Employee> pageInfo = new PageInfo<>(page);
		return pageInfo;
	}

	public List<GyUser> getAllEmploy() {
		return gyUserMapper.selectAllEmploy();
	}

	public void deleteUser(int id) {
		gyUserMapper.deleteByPrimaryKey(id);
	}

	public void addUser(GyUser user) {
		gyUserMapper.insert(user);
	}

	public void updateUser(GyUser user) {
		gyUserMapper.updateByPrimaryKey(user);
	}

	@Override
	public List<GyUser> queryUserByUsercode(String usercode) {
		return gyUserMapper.selectUserByUsercode(usercode);
	}

	@Override
	public List<GyUser> queryUserByUserName(String code) {
		return gyUserMapper.selectUserByUserName(code);
	}

	@Override
	public List<String> getAllCompany() {
		return gyUserMapper.selectAllCompany();
	}

	@Override
	public int selectUserHasExist(String usercode) {
		int count = gyUserMapper.selectUserHasExist(usercode);
		if (count == 0) {
			return 200;
		} else {
			return 400;
		}
	}

	@Override
	public String selectUsercodeByPrimaryKey(int key) {

		return gyUserMapper.selectUsercodeByPrimaryKey(key);
	}

	@Override
	public int selectCompanyIdByName(String companyName) {
		// TODO Auto-generated method stub
		return gyUserMapper.selectCompanyIdByName(companyName);
	}

	@Override
	public GyUser selectPersonByCode(String code) {
		// TODO Auto-generated method stub
		return gyUserMapper.selectPersonByCode(code);
	}

	@Override
	public String selectCompanyNameById(int companyId) {
		// TODO Auto-generated method stub
		return gyUserMapper.selectCompanyNameById(companyId);
	}

	@Override
	public List<UserNameAndCode> selectAllUserCodeAndName() {
		// TODO Auto-generated method stub
		return gyUserMapper.selectAllUserCodeAndName();
	}

	@Override
	public List<UserNameAndCode> selectAllUnRegistPerson() {
		// TODO Auto-generated method stub
		return gyUserMapper.selectAllUnRegistPerson();
	}

	@Override
	public Gy_PersonData getPersonDataByPersonCode(int personCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GyUser> getUserByMobile(String mobile) {
		return gyUserMapper.getUserByMobile(mobile);
	}

}
